package com.sakura.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.common.BaseContext;
import com.sakura.mapper.OrdersMapper;
import com.sakura.pojo.*;
import com.sakura.serivce.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    @Override
    public void submit(Orders orders) {
        // 用户ID
        Long userID = BaseContext.getCurrentId();
        User user = userService.getById(userID);
        // 主键ID
        long primaryId = IdWorker.getId();
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        // 保存Order
        orders.setId(primaryId);

        BigDecimal sum = new BigDecimal(0);
        // 保存OrderDetail
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
        shoppingCartQueryWrapper.eq("user_id",userID);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartQueryWrapper);
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setOrderId(primaryId);
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            // 单价
            orderDetail.setAmount(shoppingCart.getAmount());
            sum = sum.add(shoppingCart.getAmount());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetails.add(orderDetail);
        }
        orderDetailService.saveBatch(orderDetails);
        
        // 为了唯一性需要和主键一样
        orders.setNumber(String.valueOf(primaryId));
        orders.setStatus(2);
        orders.setUserId(userID);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(sum);
        orders.setUserName(user.getName());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setConsignee(addressBook.getConsignee());
        this.save(orders);

        // 清空购物车
        shoppingCartService.remove(shoppingCartQueryWrapper);
    }
}